package com.jbinfo.mongoot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jbinfo.mongoot.operation.visitor.Visitor;
import com.jbinfo.mongoot.util.PageModel;

@RestController
@RequestMapping("/visitor")
@Api("fuzhou-visitor类API")
public class VisitorController {
	@Autowired
	private MongoTemplate mongoTemplate;

	@ApiOperation("新增回访信息")
	@RequestMapping(value = "/insertVisitor", method = RequestMethod.POST)
	public Map<String, Object> addVisitor(@ModelAttribute Visitor visitor){
		Map<String, Object> map = new HashMap();
		String description="";

		if(visitor.getVisitScore()==null ||  "".equals(visitor.getVisitScore())){
			description+= "回访评分不能为空、";
		}
		if(visitor.getVisitType()==null ||  "".equals(visitor.getVisitType())){
			description+= "回访类型不能为空、";
		}
		if(visitor.getVisitRemark()==null ||  "".equals(visitor.getVisitRemark())){
			description+= "回访备注不能为空、";
		}
		if("".equals(description)){//非空
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			visitor.setCreatedDate(simpleDateFormat.format(new Date()));
			mongoTemplate.save(visitor);
			map.put("status", 1);
		}else{
			map.put("status", 0);
		}
		map.put("description", description);
		map.put("code", "");
		return map;
	}

	@ApiOperation("按老人cardid查询回访信息")
	@RequestMapping(value = "/findVisitorByOldid", method = RequestMethod.GET)
	public Map<String, Object> findVisitorByCardid(@RequestParam("oldid") String oldid) {
		Map<String, Object> map = new HashMap();
		String description="";
		//验证参数是否为空
		if("".equals(description)){//非空
			Query query = new Query();
			query.addCriteria(Criteria.where("oldid").is(oldid));
			List<Visitor> visitorList =  mongoTemplate.find(query, Visitor.class);
			map.put("result", visitorList);
			map.put("status", 1);
		}else{
			map.put("status", 0);
		}
		map.put("description", description);
		map.put("code", "");
		return map;
	}










	//====================================================================================================================
	//====================================================================================================================
	@ApiOperation("获得所有的VisitorList")
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public List<Visitor> getAll() {
		return mongoTemplate.find(new Query(), Visitor.class);
	}

	@ApiOperation("获得所有的VisitorList通过名称")
	@RequestMapping(value = "/getByName/{name}", method = RequestMethod.GET)
	public List<Visitor> getByName(@PathVariable("name") String name) {
		return mongoTemplate.find(new Query(new Criteria("name").is(name)), Visitor.class);
	}

	@ApiOperation("获得所有的VisitorList通过名称模糊查询")
	@RequestMapping(value = "/getByNameRegex/{name}", method = RequestMethod.GET)
	public List<Visitor> getByNameRegex(@PathVariable("name") String name) {
		Query query = new Query();
		query.addCriteria(Criteria.where("name").regex(".*?" +name+ ".*"));
		return mongoTemplate.find(query, Visitor.class);
	}

	@ApiOperation("获得所有的VisitorList通过多条件")
	@RequestMapping(value = "/getByNameByTwoCondition", method = RequestMethod.GET)
	public List<Visitor> getByNameByTwoCondition(
			@RequestParam("name") String name,
			@RequestParam("phone")String phone) {
		Query query = new Query();
		Criteria criatira = new Criteria();
		criatira.andOperator(Criteria.where("name").is(name),Criteria.where("phone").is(phone));
		return mongoTemplate.find(query.addCriteria(criatira), Visitor.class);
	}

	@ApiOperation("获得所有的VisitorList通过多条件2")
	@RequestMapping(value = "/getByNameByTwoCondition2", method = RequestMethod.GET)
	public List<Visitor> getByNameByTwoCondition2(
			@RequestParam("name") String name,
			@RequestParam("phone")String phone) {
		Query query = new Query();
		Criteria criatira = new Criteria();
		criatira.andOperator(Criteria.where("name").regex(".*?" +name+ ".*"),Criteria.where("phone").is(phone));
		return mongoTemplate.find(query.addCriteria(criatira), Visitor.class);
	}


	@ApiOperation("通过post请求添加visitor信息")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public void add(@ModelAttribute Visitor visitor) {
         mongoTemplate.insert(visitor);
    }

	@ApiOperation("删除visitor信息")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(@RequestParam("name") String name) {
         mongoTemplate.remove(new Query(new Criteria("name").is(name)));
    }

	@ApiOperation("updateFirst  修改符合条件第一条记录")
	@RequestMapping(value = "/updateFirst", method = RequestMethod.POST)
	public void updateFirst(@RequestParam("name") String name) {
		mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), new Update().set("createTime", "2015-08-08"),Visitor.class);
    }

	@ApiOperation("updateMulti  修改符合条件的所有")
	@RequestMapping(value = "/updateMulti", method = RequestMethod.POST)
	public void updateMulti(@RequestParam("name") String name) {
		mongoTemplate.updateMulti(new Query(Criteria.where("name").is(name)), new Update().set("createTime", "2015-06-08"),Visitor.class);
    }

	@ApiOperation("Upsert        修改符合条件时如果不存在则添加")
	@RequestMapping(value = "/Upsert", method = RequestMethod.POST)
	public void Upsert(@RequestParam("name") String name) {
		mongoTemplate.upsert(new Query(Criteria.where("name").is(name)), new Update().set("createTime", "2015-06-08"),Visitor.class);
    }

	@ApiOperation("getVisitorPage  获取分页1")
	@RequestMapping(value = "/getVisitorPage", method = RequestMethod.POST)
	public PageModel<Visitor> getVisitorPage(@ModelAttribute PageModel<Visitor> page) {
	   Query query=new Query();
	   //查询总数
	   int count=(int) mongoTemplate.count(query,Visitor.class);
	   page.setTotal(count);
	   //排序
	   query.with(new Sort(Direction.ASC, "name").and(new Sort(Direction.DESC,"phone")));
	   query.skip(page.getSkip()).limit(page.getPageSize());
	   List<Visitor>datas=mongoTemplate.find(query,Visitor.class);
	   page.setRows(datas);
	   return page;
	}

	@ApiOperation("getVisitorPage  获取分页2带参数")
	@RequestMapping(value = "/getVisitorPage2", method = RequestMethod.POST)
	public PageModel<Visitor> getVisitorPage2(@ModelAttribute PageModel<Visitor> page,@ModelAttribute Visitor visitor) {
	   Query query=new Query();
	   Criteria criatira = new Criteria();
//	   if(!StringUtils.isEmpty(visitor.getName())){
//		   criatira.andOperator(Criteria.where("name").regex(".*?" +visitor.getName()+ ".*"));
//	   }
//	   if(!StringUtils.isEmpty(visitor.getPhone())){
//		   criatira.andOperator(Criteria.where("phone").is(visitor.getPhone()));
//	   }
	   query.addCriteria(criatira);
	   //查询总数
	   int count=(int) mongoTemplate.count(query,Visitor.class);
	   page.setTotal(count);
	   //排序
	   query.with(new Sort(Direction.ASC, "name").and(new Sort(Direction.DESC,"phone")));
	   query.skip(page.getSkip()).limit(page.getPageSize());
	   List<Visitor>datas=mongoTemplate.find(query,Visitor.class);
	   page.setRows(datas);
	   return page;
	}
}
